package com.jinly.ryder.monitor.web;

import com.jinly.ryder.monitor.service.MonitorDataQueryService;
import com.jinly.ryder.monitor.service.model.MonitorKeyType;
import com.jinly.ryder.monitor.web.mapping.MonitorDataVOMapping;
import com.jinly.ryder.monitor.web.vo.MonitorDataVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("monitor/query")
@RequiredArgsConstructor
public class MonitorQueryController {

    private final MonitorDataVOMapping monitorDataVOMapping;
    private final MonitorDataQueryService monitorDataQueryService;

    @GetMapping
    public List<MonitorDataVO> queryDataList(@RequestParam("instanceIds") Set<Integer> instanceIds,
                                             @RequestParam("keyTypes") Set<MonitorKeyType> keyTypes,
                                             String begin,
                                             String end) {
        if (ObjectUtils.isEmpty(instanceIds) || ObjectUtils.isEmpty(begin) || ObjectUtils.isEmpty(end))
            return Collections.EMPTY_LIST;
        LocalDateTime beginTime = LocalDateTime.parse(begin, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return monitorDataVOMapping.toVoList(monitorDataQueryService.queryData(instanceIds, keyTypes, beginTime, endTime));
    }
}
